package com.zx.idc.backend.gui.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.shiro.TokenManagerUtil;
import com.zx.idc.ds.resource.pojo.MirrorCardInfo;
import com.zx.idc.ds.resource.pojo.ServerInfo;
import com.zx.idc.ds.sys.bean.TotalResult;
import com.zx.idc.ds.sys.bean.UserInfo;
import com.zx.idc.ds.sys.entity.SysRole;
import com.zx.idc.ds.sys.entity.SysUser;
import com.zx.idc.ds.sys.service.ISysRoleService;
import com.zx.idc.ds.sys.service.IndexPageService;
import com.zx.idc.ds.tlog.entity.TLogSecurity;
import com.zx.idc.ds.tlog.service.ITLogSecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 首页 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-04-09
 */
@RestController
@RequestMapping("/api/index")
public class IndexPageController extends BaseApi {

    @Autowired
    private IndexPageService indexPageService;

    @Autowired
    private TokenManagerUtil tokenManagerUtil;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ITLogSecurityService itLogSecurityService;

    @GetMapping("/total")
    public Result<TotalResult> total() {
        TotalResult totalResult = indexPageService.total();
        return success(totalResult);
    }

    /**
     * 获取所有服务器资源统计信息
     * @return
     */
    @GetMapping("/serverInfos")
    public Result<List<ServerInfo>> serverInfos() {
        List<ServerInfo> serverInfos = indexPageService.getAllServerInfos();
        return success(serverInfos);
    }

    @GetMapping("/get-login-info/")
    public Result<UserInfo> userInfo() {
        SysUser currentUser = tokenManagerUtil.getCurrentUser();
        //获取上次登录时间和IP
        TLogSecurity tLogSecurity = itLogSecurityService.selectOne(
            new EntityWrapper<>(
                new TLogSecurity()
                    .setUserID(currentUser.getId())
                    .setEventType(TLogSecurity.LOGOUT)
            )
                .orderBy("actTime", false)
                .last("limit 1")
        );

        List<SysRole> roles = iSysRoleService
            .getRolesByUserId(currentUser.getId());
        String roleName = null;
        if (roles != null && !roles.isEmpty()) {
            roleName = roles.get(0).getName();
        }

        String lastLoginIp = null;
        LocalDateTime lastLoginTime = null;
        if (tLogSecurity != null) {
            lastLoginIp = tLogSecurity.getSourceIp();
            lastLoginTime = tLogSecurity.getActTime();
        }

        return success(
            new UserInfo()
                .setUserName(currentUser.getLoginName())
                .setLastLoginTime(lastLoginTime)
                .setLastLoginIp(lastLoginIp)
                .setRoleName(roleName)
        );
    }


    /**
     * 收集镜像网卡的状态信息
     * @return
     */
    @GetMapping("/collectMirrorCardsStatus")
    public Result<List<MirrorCardInfo>> collectMirrorCardsStatus() {
        return success(indexPageService.collectMirrorCardsStatus());
    }

}

